#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include "ni488.h"

#define ARRAYSIZE 100            // Size of read buffer
int Dev; // Device handle
char ReadBuffer[ARRAYSIZE + 1]; // Read data buffer
char ErrorMnemonic[21][5] = { "EDVR", "ECIC", "ENOL", "EADR", "EARG", "ESAC",
		"EABO", "ENEB", "EDMA", "", "EOIP", "ECAP", "EFSO", "", "EBUS", "ESTB",
		"ESRQ", "", "", "", "ETAB" };
void GPIBCleanup(int Dev, char* ErrorMsg);

int _cdecl main(void)
{
	char Str1[]="APPL:SIN ";
	char Str2[]=",3";
	char Str_temp[20];
	char Str_i[7]=" ";

	Dev=ibfind("GPIB0");
	if(ibsta&ERR)
	{
		GPIBCleanup(Dev,"Unable to open board");
		return 1;
	}

	ibsic(Dev);
	ibsre(Dev,1);
	ibcmd(Dev,"%!",3L);
	ibcmd(Dev,"\04",1L); //清空

	for(int i=1000;i<=100000;i+=1000)
	{
		Str_temp[0]='\0';
		itoa(i,Str_i,10);
		strcat(Str_temp,Str1);
		strcat(Str_temp,Str_i);
		strcat(Str_temp,Str2);

		ibcmd(Dev,"?@%",3L); //信号发生
		ibwrt(Dev,Str_temp,20L);
		printf("%s\n",Str_temp);

		ibcmd(Dev,"?@!",3L); //有效值
		ibwrt(Dev,":MEASURE:Vrms?",14L);

		ibcmd(Dev,"?%!",3L); //群触发
		ibcmd(Dev,"\08",1L);

		ibcmd(Dev,"?A ",3L); //读取
		ibrd(Dev,ReadBuffer,ARRAYSIZE);
		ReadBuffer[ibcnt]='\0';
		printf("Data read:%s\n",ReadBuffer);
	}

	ibonl(Dev,0);
	return 0;
}

void GPIBCleanup(int ud, char* ErrorMsg) {
	printf("Error : %s\nibsta = 0x%x iberr = %d (%s)\n", ErrorMsg, ibsta,
			iberr, ErrorMnemonic[iberr]);
	if (ud != -1) {
		printf("Cleanup: Taking device offline\n");
		ibonl(ud, 0);
	}
}

